package project001.view;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import project001.properties.LanguageResources;

public class FrameUserAccount extends JFrame {

	private static final long serialVersionUID = 1L;
	private Container mainPanel;
	private JPanel userFormPanel;
	private JLabel labelNameUser;
	private JTextField fieldNameUser;
	private JLabel labelPasswordUser;
	private JPasswordField fieldPasswordUser;
	private JButton buttonLogin;

	public FrameUserAccount() {
		initFrame();
	}
	
	public void initFrame() {
		setTitle(LanguageResources.getResource("system"));
		ImageIcon icon = new ImageIcon(getClass().getResource("/project001/icons/icon-security.png"));
		setSize(800, 600);
		setResizable(false);
		//setExtendedState(MAXIMIZED_BOTH);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setIconImage(icon.getImage());
		mainPanel = getContentPane();
		GridBagLayout borderLayout = new GridBagLayout();
		mainPanel.setLayout(borderLayout);
		
		/*addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if (Messages.getMessageOptionCloseSystem()) {
					dispose();
				} else {
					setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
				}
			}
		});*/
		
		Constraints constraints = new Constraints().setAnchor(GridBagConstraints.NORTHEAST)
				.setFill(GridBagConstraints.NONE)
				.setGridyGridx(0, 0)
				.setWeightxWeighty(1, 1);
		mainPanel.add(getUserPanel(), constraints);
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		userFormPanel.setLayout(gridBagLayout);
		
		constraints = new Constraints().setAnchor(GridBagConstraints.NORTHWEST)
				.setGridyGridx(0, 0);
		userFormPanel.add(getLabelNameUser(), constraints);
		
		constraints = new Constraints().setAnchor(GridBagConstraints.NORTHEAST)
				.setGridyGridx(1, 0);
		userFormPanel.add(getFieldNameUser(), constraints);	
		
		constraints = new Constraints().setAnchor(GridBagConstraints.NORTHWEST)
				.setGridyGridx(2, 0);
		userFormPanel.add(getLabelPasswordUser(), constraints);
		
		constraints = new Constraints().setAnchor(GridBagConstraints.NORTHEAST)
				.setGridyGridx(3, 0);
		userFormPanel.add(getFieldPasswordUser(), constraints);
		
		constraints = new Constraints().setFill(GridBagConstraints.NONE)
				.setAnchor(GridBagConstraints.WEST)
				.setGridyGridx(4, 0)
				.setIpadxIpadY(40, 0);
		userFormPanel.add(getButtonLogin(), constraints);
	}
	
	private JPanel getUserPanel() {
		if (userFormPanel == null) {
			userFormPanel = new JPanel();
			userFormPanel.setBorder(new TitledBorder(new LineBorder(Color.GRAY, 1, true)
			, LanguageResources.getResource("user_account")));
		}
		return userFormPanel;
	}
	
	private JLabel getLabelNameUser() {
		if (labelNameUser == null) {
			labelNameUser = new JLabel(LanguageResources.getResource("user_name")  + "                          ");
		}
		return labelNameUser;
	}
	
	private JTextField getFieldNameUser() {
		if (fieldNameUser == null) {
			fieldNameUser = new JTextField();
			fieldNameUser.setDocument(new FormatTextField(50, "l"));
			
			fieldNameUser.addKeyListener(new KeyListener() {
				
				@Override
				public void keyTyped(KeyEvent arg0) {}
				
				@Override
				public void keyReleased(KeyEvent arg0) {
					if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
						getFieldPasswordUser().requestFocus();						
					}
				}
				
				@Override
				public void keyPressed(KeyEvent arg0) {}
			});
		}
		return fieldNameUser;
	}
	
	private JLabel getLabelPasswordUser() {
		if (labelPasswordUser == null) {
			labelPasswordUser = new JLabel(LanguageResources.getResource("password_name")  + "                          ");
		}
		return labelPasswordUser;
	}
	
	private JPasswordField getFieldPasswordUser() {
		if (fieldPasswordUser == null) {
			fieldPasswordUser = new JPasswordField();
			fieldPasswordUser.setDocument(new FormatTextField(50, ""));
			
			fieldPasswordUser.addKeyListener(new KeyListener() {
				
				@Override
				public void keyTyped(KeyEvent arg0) {}
				
				@Override
				public void keyReleased(KeyEvent arg0) {
					if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
						getButtonLogin().requestFocus();
					}
				}
				
				@Override
				public void keyPressed(KeyEvent arg0) {}
			});
		}
		return fieldPasswordUser;
	}
	
	private JButton getButtonLogin() {
		if (buttonLogin == null) {
			buttonLogin = new JButton(LanguageResources.getResource("login"));			
			buttonLogin.addKeyListener(new KeyListener() {
				
				@Override
				public void keyTyped(KeyEvent e) {}
				
				@Override
				public void keyReleased(KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) {
						System.out.println(getFieldPasswordUser().getPassword());
					}
				}
				
				@Override
				public void keyPressed(KeyEvent e) {}
			});
			
			buttonLogin.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					System.out.println(getFieldPasswordUser().getPassword());
					
				}
			});
		}
		return buttonLogin;
	}
	
}
